
Ext.namespace("admin.student");

admin.student.clazz = function(panelId, title, iconClz){

	var clazzStore = new Ext.data.JsonStore({
		url : "admin/clazz/findClazzByCon",
		totalProperty : "total",
		root : "rows",
		autoLoad : true,
		fields : ["id", "name", "studentNumber", "status", "enterSchoolDate", "graduateDate",
			"createDate", "updateDate", "des", "authorName", "owner"
		],
		listeners : {
			beforeload : function(s){
				s.removeAll();
				Ext.apply(s.baseParams, {
					status : statusCombo.getValue(),
					con : queryField.getValue()
				});
			}
		}
	});
	var clazzCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
		header : "班级名称",
		dataIndex : "name",
		width : 180
	}, {
		header : "状态",
		dataIndex : "status",
		renderer : renderStatus
	}, {
		header : "人数",
		dataIndex : "studentNumber"
	}, {
		header : "入校时间",
		dataIndex : "enterSchoolDate",
		renderer : renderDate
	}, {
		header : "毕业时间",
		dataIndex : "graduateDate",
		renderer : renderDate
	}, {
		header : "创建时间",
		dataIndex : "createDate",
		renderer : renderDate
	}, {
		header : "更新时间",
		dataIndex : "updateDate",
		renderer : renderDate
	}, {
		header : "描述",
		dataIndex : "des"
	}]);
	var clazzSm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	var optBar = genOptBarByFn(create, del, update, toggle);
	var statusCombo = genStatusCombo();
	var queryField = genQueryField();
	var queryBar = ["->", "状态:", statusCombo, "关键字:", queryField, {
		text : "查询",
		iconCls : "query",
		handler : function(){
			clazzStore.load();
		}
	}];
	var clazzGrid = new Ext.grid.GridPanel({
		sm : clazzSm,
		cm : clazzCm,
		store : clazzStore,
		tbar : genToolBar(optBar, queryBar, panelId)
	});
	return new Ext.Panel({
		id : panelId,
		title : title,
		iconCls : iconClz,
		closable : true,
		layout : "fit",
		items : [clazzGrid]
	});

	function create(){
		var win = genAddOrUpdateWin("admin/clazz/add");
		win.show();
	}
	function del(){
		var rec = clazzSm.getSelected();
		if(rec==null){
			showFailMsg("请选择班级.");
			return;
		}
		confirmDelete(function(){
			ajaxRequest({
				url : "admin/clazz/delete/" + rec.get("id"),
				callback : function(){
					clazzStore.load();
				}
			});
		});
	}
	function update(){
		var rec = clazzSm.getSelected();
		if(rec==null){
			showFailMsg("请选择班级.");
			return;
		}
		var win = genAddOrUpdateWin("admin/clazz/update");
		win.findByType("form")[0].getForm().loadRecord(rec);
		win.show();
	}
	function toggle(){
		var rec = clazzSm.getSelected();
		if(rec==null){
			showFailMsg("请选择班级.");
			return;
		}
		ajaxRequest({
			url : "admin/clazz/toggleStatus/" + rec.get("id"),
			callback : function(){
				clazzStore.load();
			}
		});
	}
	function genAddOrUpdateWin(url){
		return new Ext.Window({
			title : "新增或更新班级",
			layout : "fit",
			modal : true,
			iconCls : "save",
			width : 300,
			height : 200,
			plain : true,
			bodyStyle : 'padding:5px;',
			items : genForm(url)
		});
		function genForm(url){
			return new Ext.form.FormPanel({
				url : url,
				labelWidth : 70,
				labelAlign : "right",
				baseCls: 'x-plain',
				defaults : {
					xtype : "textfield",
					anchor : "95%"
				},
				items : [{
					xtype : "hidden",
					name : "id",
					value : "-1"
				}, {
					fieldLabel:"班级名称",
					name : "name"
				}, genStatusCombo(), {
					fieldLabel : "描述",
					name : "des",
					xtype : "textarea"
				}],
				buttonAlign : "center",
				buttons : [{
					text : "保存",
					iconCls : "save",
					handler : function(b){
						submitForm(b.findParentByType("form"), refreshGrid);
						function refreshGrid(){
							clazzStore.load();
						}
					}
				}, {
					text : "重置",
					iconCls : "reset",
					type : "reset",
					handler : function(b){
						b.findParentByType("form").getForm().reset();
					}
				}]
			});
		}
	}
}

registerModule("admin.student.clazz", admin.student.clazz);
